<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
namespace Mcm\UserBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Mcm\RepasBundle\Entity\Repas;
use Mcm\RepasBundle\Entity\Bon;
use Mcm\RepasBundle\Entity\Paiement;
use Doctrine\ORM\EntityRepository as EntityRepository;
use Mcm\RepasBundle\Form\CoderepasType;
use Mcm\RepasBundle\Form\CoderepasHandler;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
/**
 * Description of CompteController
 *
 * @author Gael
 */
class CompteController extends Controller {
    
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();
        $user = $this->container->get('security.context')->getToken()->getUser();
        $repas_de_l_hote = $em->getRepository('McmRepasBundle:Repas')->findByRepasHote($user);
        $repas_de_l_hote_later = $em->getRepository('McmRepasBundle:Repas')->findByRepasHoteLater($user);
        $paiements_invite = $em->getRepository('McmRepasBundle:Paiement')->findByRepasInvite($user);
        $repas_de_l_invite=array();
        foreach ($paiements_invite as $paiement) {
            $repas_de_l_invite[]=$paiement->getRepas();
        }
       
                            
        return $this->render('McmUserBundle:Compte:index.html.twig', array('repas_hotes' => $repas_de_l_hote,
                                                                            'repas_hotes_later'=>$repas_de_l_hote_later,
                                                                            'repas_invite'=>$repas_de_l_invite,
                                                                            'msg'=>null
                                                                            ));
    }
    public function ajoutcoderepasAction($id){
        $em = $this->getDoctrine()->getManager();
        $repas = $em->getRepository('McmRepasBundle:Repas')->find($id);
        $form = $this->createForm(new CoderepasType($id));
        return $this->render('McmUserBundle:Compte:ajoutcoderepas.html.twig', array(
                    'form' => $form->createView(),
                    'repas' => $repas
                ));
    } 
    public function enrcoderepasAction(){
        $user = $this->container->get('security.context')->getToken()->getUser();
        $form = $this->createForm(new CoderepasType(0));
        $formHandler = new CoderepasHandler($form, $this->get('request'), $this->getDoctrine()->getEntityManager(),$user);
        if($formHandler->process()){
          return $this->render('McmUserBundle:Compte:enrcoderepas.html.twig', array(
                    'msg' => "solde crédité"
                    
                ));  
        }else{
            return $this->render('McmUserBundle:Compte:enrcoderepas.html.twig', array(
                    'msg' => "code inexistant"
                    
                )); 
        }
    }
    
    public function acompteAction(){
        $user = $this->container->get('security.context')->getToken()->getUser();
        
        $defaultData = array('message' => '...');
         $form = $this->createFormBuilder($defaultData)
        ->add('enseigne', 'entity', array(
        'class' => 'McmRepasBundle:Enseigne',
            'property'=>'nom',
        'query_builder' => function(EntityRepository $er) {
                            return $er->createQueryBuilder('e')->orderBy('e.nom', 'ASC');}))
        ->getForm();

       
        return $this->render('McmUserBundle:Compte:acompte.html.twig', array(
                    'user' => $user,
                    'form'=>$form->createView()
                    
                )); 
    }
    public function getbonAction(Request $request){
        $user = $this->container->get('security.context')->getToken()->getUser();
        $defaultData = array('message' => '...');
        $form = $this->createFormBuilder($defaultData)
        ->add('enseigne', 'entity', array(
        'class' => 'McmRepasBundle:Enseigne',
            'property'=>'nom',
        'query_builder' => function(EntityRepository $er) {
                            return $er->createQueryBuilder('e')->orderBy('e.nom', 'ASC');}))
        ->getForm();
         
        
        
        $thumb = imagecreatetruecolor(500, 300);
        $fond = imagecolorallocate($thumb, 255, 0, 255); // fond
        imagefill($thumb, 0, 0, $fond);
        $codebon ="";
            
            while (strlen($codebon)< 8) {
                $codebon .= mt_rand(0, 9);
            }
            $bon = new \Mcm\RepasBundle\Entity\Bon();
            
            $bon->setCode($codebon);
            $bon->setMontant($user->getAcompte());
            
            if ($request->isMethod('POST')) {
            $form->bind($request);
//            $this->get('request')->request->get('enseigne')
           
            
            $data = $form->getData();
            $bon->setEnseigne($data['enseigne']);
//                $bon->setEnseigne($this->getDoctrine()->getRepository('McmRepasBundle:Enseigne')->find($data['enseigne']	));
            }
        
            
        
        $TabCode = array(
            '0' =>  '101000111011101',
            '1' =>  '111010001010111',
            '2' =>  '101110001010111',
            '3' =>  '111011100010101',
            '4' =>  '101000111010111',
            '5' =>  '111010001110101',
            '6' =>  '101110001110101',
            '7' =>  '101000101110111',
            '8' =>  '111010001011101',
            '9' =>  '101110001011101',
            '*' =>  '100010111011101');
        
            $CodeBar = '';
            $TabKeys = array_keys($TabCode);
            $TotalChar = 0;
        for($i = 0; $i < strlen($codebon); $i++)
        {
            $CodeBar .= "0" . $TabCode[$codebon[$i]] . "<br>";

            //récupération de l'index du caractère + calcul de la somme des indexes
            foreach($TabKeys as $key => $value) {
                if($value === $codebon[$i]){ $TotalChar += $key;}
            }
        }
        $IndexControl = bcmod($TotalChar, 43);
       
            $CodeBar.= "0" . $TabCode[$TabKeys[$IndexControl]];
        
        $xCodeBar = $TabCode["*"] . $CodeBar . "0" . $TabCode["*"];
        
        $c_w = imagecolorallocate($thumb, 255, 255, 255); // blanc
        $c_b = imagecolorallocate($thumb, 0, 0, 0); // noir
        
        $x_ref = 0;
        for($x=0; $x < strlen($xCodeBar); $x++)
        {
            if($xCodeBar[$x] == "1")
            {
                imageline($thumb, $x_ref, 0, $x_ref, 25, $c_b);
            }
            else
            {
                imageline($thumb, $x_ref, 0, $x_ref, 25, $c_w);
            }
            $x_ref++;
        }
        
        
        $IMG = imagecreatetruecolor($x_ref, 25);
        imagefill($IMG, 0, 0, $fond);
        imagecopymerge ($IMG, $thumb, 0, 0, 0, 0, 500, 25, 100 );
        
        
        imagepng($IMG, $bon->getUploadDir());
        $em = $this->getDoctrine()->getManager();
        $bon->setPng($bon->getUploadDir());
         $bon->setHote($user);
        $em->persist($bon);
        $em->merge($user);
        $em->flush();
        
        
        return $this->render('McmUserBundle:Compte:bon.html.twig', array(
                    'user' => $user,
                    'bon'=>$bon,
                    'chemin'=>$bon->getPath()
                )); 
        
    }
}

?>
